<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="版本描述" prop="versionDesc">
        <el-input
          v-model="queryParams.versionDesc"
          placeholder="请输入版本描述"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      
      
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button> 
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:version:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit" 
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:version:edit']"
        >修改</el-button>
      </el-col>
        
    </el-row>

    <el-table v-loading="loading" :data="versionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" align="center" /> 
      <el-table-column label="版本类型" align="center" prop="appVersion" />
      <el-table-column label="版本描述" align="center" prop="versionDesc" />
      <el-table-column label="升级描述" align="center" prop="description" />
      <el-table-column label="下载地址" align="center" prop="downPath" />
      <el-table-column label="是否强制更新" align="center" prop="updateWay">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.updateWay"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button 
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:version:edit']"
          >修改</el-button>
          <el-button 
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:version:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :v-model:page="queryParams.pageNum"
         :v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改app更新接口管理对话框 -->
    <el-dialog :title="title" v-model="open"  width="860px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="版本类型" prop="appVersion">
          <el-input v-model="form.appVersion" type="number"  placeholder="请输入版本类型" />
        </el-form-item>
        <el-form-item label="版本描述" prop="versionDesc">
          <el-input v-model="form.versionDesc" placeholder="请输入版本描述" />
        </el-form-item>
        <el-form-item label="升级描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下载地址" prop="downPath">
          <FileBottonUpload ref="FileBottonUpload" v-model="form.downPath" ></FileBottonUpload>
       
        </el-form-item>
        <el-form-item label="是否强制更新">
          <el-radio-group v-model="form.updateWay">
            <el-radio
              v-for="dict in sys_yes_no"
              :key="dict.value"
:label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
      <div   class="dialog-footer">
        <el-button type="primary"  @click="submitForm">确 定</el-button>
        <el-button @click="cancel" >取 消</el-button>
      </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="version">
const { proxy } = getCurrentInstance();
import { listVersion,  addVersion, updateVersion } from "@/api/system/version";

const { sys_yes_no } = proxy.useDict("sys_yes_no");
 

const versionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({  
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appVersion: undefined,
        versionDesc: undefined,
        description: undefined,
        downPath: undefined,
        updateWay: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }  
});
const { queryParams, form, rules } = toRefs(data);


 
 
 
    /** 查询app更新接口管理列表 */
  function  getList() {
    loading.value = true;
      listVersion(proxy.queryParams).then(response => {
        versionList.value = response.rows;
        total.value= response.total;
        loading.value = false;
      });
    }
    // 取消按钮
    function   cancel() {
       open.value  = false;
       reset();
    };
    // 表单重置
    function  reset() {
      form.value = {
        id: null,
        appVersion: null,
        versionDesc: null,
        description: null,
        downPath: null,
        updateWay: "Y",
        updateBy: null,
        updateTime: null
      }; 
      proxy.resetForm("formRef");
    };

   
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.configId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


    /** 新增按钮操作 */
    function  handleAdd() {

      reset();
  open.value = true;
  title.value = "添加app更新接口管理"; 
    }


/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const configId = row.id || ids.value;
  getConfig(configId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改app更新接口管理";
  });
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      if (form.value.configId != undefined) {
        updateVersion(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVersion(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
 

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || this.ids;
  proxy.$modal.confirm('是否确认删除参数编号为"' + configIds + '"的数据项？').then(function () {
    return delVersion(configIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}
 
  getList();
</script>
